package main

import (
	"code.byted.org/gopkg/gorm"
	"fmt"
	"log"
)

func DbConn(MyUser, Password, Host, Db string, Port int) *gorm.DB {
	connArgs := fmt.Sprintf("%s:%s@(%s:%d)/%s?charset=utf8&parseTime=True&loc=Local", MyUser, Password, Host, Port, Db)
	db, err := gorm.Open("mysql", connArgs)
	if err != nil {
		log.Fatal(err)
	}
	db.SingularTable(true)
	return db
}

//定义数据库连接
type ConnInfo struct {
	MyUser   string
	Password string
	Host     string
	Port     int
	Db       string
}

func main() {
	cn := ConnInfo{
		"root",
		"123456",
		"127.0.0.1",
		3306,
		"xd_data",
	}
	db := DbConn(cn.MyUser, cn.Password, cn.Host, cn.Db, cn.Port)
	// 关闭数据库链接，defer会在函数结束时关闭数据库连接
	defer db.Close()

}
